home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / citsrc6K05.lha / libnet.c < prev    next >
C/C++ Source or Header  |  1996-08-29  |  4KB  |  193 lines

  1. /*
  2. *                               libnet.c
  3. *
  4. * Library net functions.
  5. */
  6. /*
  7. *                               history
  8. *
  9. * 85Nov15 HAW  Created.
  10. */
  11. #include "ctdl.h"
  12. NetBuffer        netBuf;
  13. NetBuffer        netTemp;
  14. extern NetTable  *netTab;
  15. extern CONFIG    cfg;
  16. int              thisNet = -1;
  17. FILE             *netfl;
  18. /*
  19. *                               contents
  20. *
  21. *       getNet()                gets a node from CTDLNET.SYS
  22. *       putNet()                puts a node to CTDLNET.SYS
  23. *       normId()                normalizes the ID
  24. *       searchNet()             search net on ID
  25. *       searchNameNet()         search net on Name
  26. */
  27. extern FILE *netLog;
  28. /*
  29. * getNet()
  30. *
  31. * This function gets a net node from the file.
  32. */
  33. void getNet(int n, NetBuffer *buf)
  34.   {
  35.   long int r, s;
  36.   char error[50];
  37.   if (buf == &netBuf)
  38.   thisNet = n;
  39.   r = NB_TOTAL_SIZE;
  40.   s = n * r;
  41.   fseek(netfl, s, 0);
  42.   if (fread(buf, NB_SIZE, 1, netfl) != 1)
  43.     {
  44.     sPrintf(error, "?getNet-read fail(1) for slot %d.", n);
  45.     crashout(error);
  46.  
  47.     }
  48.   crypte(buf, NB_SIZE, n);
  49.   if (SR_BULK != 0)
  50.     {
  51.     if (fread(buf->netRooms, SR_BULK, 1, netfl) != 1)
  52.       {
  53.       sPrintf(error, "?getNet-read fail(2) for slot %d.", n);
  54.       crashout(error);
  55.  
  56.       }
  57.  
  58.     }
  59.  
  60.   }
  61. /*
  62. * putNet()
  63. *
  64. * This will put a node to file.  It also updates the net index.
  65. */
  66. void putNet(int n, NetBuffer *buf)
  67.   {
  68.   long int r, s;
  69.   label temp;
  70.   char error[50];
  71.   if (buf == &netBuf) thisNet = n;
  72.   copy_struct(buf->nbflags, netTab[n].ntflags);
  73.   memcpy(netTab[n].netTRooms, buf->netRooms, (long)SR_BULK);
  74.   netTab[n].ntnmhash = hash(buf->netName);
  75.   normId(buf->netId, temp);
  76.   netTab[n].ntidhash = hash(temp);
  77.   netTab[n].ntMemberNets = buf->MemberNets;
  78.   strCpy(netTab[n].ntShort, buf->nbShort);
  79.   netTab[n].ntGen = buf->nbGen;
  80.   r = NB_TOTAL_SIZE;
  81.   s = n * r;
  82.   crypte(buf, NB_SIZE, n);
  83.   fseek(netfl, s, 0);
  84.   if (fwrite(buf, NB_SIZE, 1, netfl) != 1)
  85.     {
  86.     sPrintf(error, "?putNet-write fail(1), slot %d!!", n);
  87.     crashout(error);
  88.  
  89.     }
  90.   if (SR_BULK != 0)
  91.   if (fwrite(buf->netRooms, SR_BULK, 1, netfl) != 1)
  92.     {
  93.     sPrintf(error, "?putNet-write fail(2), slot %d!!", n);
  94.     crashout(error);
  95.  
  96.     }
  97.   crypte(buf, NB_SIZE, n);
  98.  
  99.   }
  100. /*
  101. * normId()
  102. *
  103. * This normalizes a node id, i.e., kills all the punctuation and uppercases
  104. * the country designation.
  105. */
  106. char normId(label source, label dest)
  107.   {
  108.   int digitcount = 0;
  109.   while (!isalpha(*source) && *source)
  110.   source++;
  111.   if (!*source) return FALSE;
  112.   *dest++ = toUpper(*source++);
  113.   while (!isalpha(*source) && *source)
  114.   source++;
  115.   if (!*source) return FALSE;
  116.   *dest++ = toUpper(*source++);
  117.   while (*source)
  118.     {
  119.     if (isdigit(*source))
  120.       {
  121.       *dest++ = *source;
  122.       digitcount++;
  123.  
  124.       }
  125.     source++;
  126.  
  127.     }
  128.   *dest = '\0';
  129.   return (digitcount > 8);
  130.  
  131.   }
  132. /*
  133. * searchNet()
  134. *
  135. * This function Searches the net for the given Id.
  136. */
  137. int searchNet(char *forId, NetBuffer *buf)
  138.   {
  139.   int   rover;
  140.   label temp, temp2;
  141.   normId(forId, temp2);
  142.   for (rover = 0; rover < cfg.netSize; rover++)
  143.     {
  144.     if (netTab[rover].ntflags.in_use &&
  145.     hash(temp2) == netTab[rover].ntidhash)
  146.       {
  147.       getNet(rover, buf);
  148.       normId(buf->netId, temp);
  149.       if (strCmpU(temp, temp2) == SAMESTRING)
  150.       return rover;
  151.  
  152.       }
  153.  
  154.     }
  155.   return ERROR;
  156.  
  157.   }
  158. /*
  159. * searchNameNet()
  160. *
  161. * This function will search the  net for given node name.  This includes checks
  162. * for both full and shorthand names.
  163. */
  164. int searchNameNet(label name, NetBuffer *buf)
  165.   {
  166.   int rover;
  167.   if (strlen(name) == 0) return ERROR;
  168.   /* First check for full name */
  169.   for (rover = 0; rover < cfg.netSize; rover++)
  170.     {
  171.     if (netTab[rover].ntflags.in_use &&
  172.     hash(name) == netTab[rover].ntnmhash)
  173.       {
  174.       getNet(rover, buf);
  175.       if (strCmpU(buf->netName, name) == SAMESTRING)
  176.       return rover;
  177.  
  178.       }
  179.  
  180.     }
  181.   /* Now check for shorthand name */
  182.   for (rover = 0; rover < cfg.netSize; rover++)
  183.   if (netTab[rover].ntflags.in_use &&
  184.   strCmpU(netTab[rover].ntShort, name) == SAMESTRING)
  185.     {
  186.     getNet(rover, buf);
  187.     return rover;
  188.  
  189.     }
  190.   return ERROR;
  191.  
  192.   }
  193.